package oaSystem.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import oaSystem.biz.AnnouncementBiz;
import oaSystem.biz.DepartmentBiz;
import oaSystem.biz.EmployeeBiz;
import oaSystem.entity.Announcement;
import oaSystem.entity.Department;
import oaSystem.entity.Employee;
import oaSystem.web.security.Authorize;

@Controller
public class AnnouncementController {
	@Autowired
	private AnnouncementBiz announcementBiz;
	
	@Autowired
	private EmployeeBiz employeeBiz;
	
	@Autowired
	private DepartmentBiz departmentBiz;
	/**
	 * 查看我的公告
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param session
	 * @param model
	 * @return
	 */
	@Authorize(rules="announcement-mine")
	@RequestMapping(value="/announcement/mine",method=RequestMethod.GET)
	public String showMyAnnouncement(
			@RequestParam(value="status",defaultValue="2") int status,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		searchAnnouncementByStatus(status, keyword, pageNum, session, model);
		return "announcement-mine";
	}
	
	/**
	 * 方法抽取（查询公告By Status）
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param session
	 * @param model
	 */
	private void searchAnnouncementByStatus(int status, String keyword, int pageNum, HttpSession session, Model model) {
		Employee employee = (Employee)session.getAttribute("user");
		int userId = employee.getId();
		int pageSize = 10;
		List<Announcement> announcement = announcementBiz.findAnnouncementByCreator(userId, status, keyword, pageNum, pageSize);
		Integer totalCount = announcementBiz.findCountByCreator(userId, status, keyword);
		totalCount = totalCount==null?0:totalCount;
		
		int totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("keyword", keyword);
		model.addAttribute("status", status);
		model.addAttribute("announcement", announcement);
	}
	
	/**
	 * 公告草拟
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param model
	 * @param userId
	 * @param pageSize
	 */
	@Authorize(rules="announcement-draft")
	@RequestMapping(value="/announcement/draft",method=RequestMethod.GET)
	public String announcement_draft(
			@RequestParam(value="status",defaultValue="0") int status,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		searchAnnouncementByStatus(status, keyword, pageNum, session, model);
		return "announcement-mine";
	}
	/**
	 * 待审批
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param model
	 * @param userId
	 * @param pageSize
	 */
	@Authorize(rules="announcement-waitpass")
	@RequestMapping(value="/announcement/waitpass",method=RequestMethod.GET)
	public String announcement_waitpass(
			@RequestParam(value="status",defaultValue="1") int status,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		searchAnnouncementByStatus(status, keyword, pageNum, session, model);
		return "announcement-mine";
	}
	/**
	 * 通过审批
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param model
	 * @param userId
	 * @param pageSize
	 */
	@Authorize(rules="announcement-passed")
	@RequestMapping(value="/announcement/passed",method=RequestMethod.GET)
	public String announcement_passed(
			@RequestParam(value="status",defaultValue="2") int status,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		searchAnnouncementByStatus(status, keyword, pageNum, session, model);
		return "announcement-mine";
	}
	/**
	 * 公告驳回
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param model
	 * @param userId
	 * @param pageSize
	 */
	@Authorize(rules="annoucement-notpassed")
	@RequestMapping(value="/announcement/notpassed",method=RequestMethod.GET)
	public String announcement_notpassed(
			@RequestParam(value="status",defaultValue="3") int status,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		searchAnnouncementByStatus(status, keyword, pageNum, session, model);
		return "announcement-mine";
	}

	/**
	 * 公告注销
	 * @param status
	 * @param keyword
	 * @param pageNum
	 * @param model
	 * @param userId
	 * @param pageSize
	 */
	@Authorize(rules="annoucement-logout")
	@RequestMapping(value="/annoucement/logout",method=RequestMethod.GET)
	public String announcement_logout(
			@RequestParam(value="status",defaultValue="4") int status,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		searchAnnouncementByStatus(status, keyword, pageNum, session, model);
		return "announcement-mine";
	}

	
	
	/**
	 * 增加我的公告
	 * @param announcement
	 * @param session
	 * @return
	 */
	@Authorize(rules="announcement-add")
	@RequestMapping(value="/announcement/mine",method=RequestMethod.POST)
	public String addMyAnnouncement(Announcement announcement,HttpSession session){//新添加
		Employee employee = (Employee)session.getAttribute("user");//当前申请人
		//此时announcement title和content已经填写
		announcement.setCreator(employee);//申请人
		Department department = departmentBiz.fetchDepartmentById(employee.getDepartment().getId());//部门
		List<Employee> employeeList = department.getEmployeeList();
		Employee approver = null;
		for (Employee e : employeeList) {
			if(e.getIsManager()==true)
				approver=e;
		}
		announcement.setApprover(approver);//审批人
		announcementBiz.addAnnouncement(announcement);//先存入草稿箱
		Integer status = announcement.getStatus();
		if(status==1){
			announcementBiz.submitAnnouncement(announcement);//发送 改变 状态status为1  而且向经理发邮件
		}
		return "redirect:/announcement/mine?parentId=62&status="+status;//添加草稿箱或者 直接发送
	}
	/**
	 * 编辑我的公告
	 * @param announcement
	 * @param session
	 * @return
	 */
	@Authorize(rules="announcement-edit")
	@RequestMapping(value="/announcement/mine-edit",method=RequestMethod.POST)
	public String editMyAnnouncement(Announcement announcement,HttpSession session){//编辑
		//此时announcement id title和content createTime已经填写
		Employee creator = (Employee)session.getAttribute("user");
		Date createTime = announcementBiz.fetchAnnouncementById(announcement.getId()).getCreateTime();
		announcement.setCreateTime(createTime);//创建时间  这里因为字段不能多过  所以才 靠Id先查出来
		announcement.setCreator(creator);//申请人
		Department department = departmentBiz.fetchDepartmentById(creator.getDepartment().getId());//通过部门id获取部门
		List<Employee> employeeList = department.getEmployeeList();
		Employee approver = null;
		for (Employee e : employeeList) {
			if(e.getIsManager()==true)
				approver = e;
		}
		announcement.setApprover(approver);//审批人
		announcement.setLastEditTime(new Date());//最后更新时间
		Integer status = announcement.getStatus();
		if(status==1){
			announcementBiz.submitAnnouncement(announcement);//发送
		}else{
			announcementBiz.updateDrafts(announcement);//更新草稿箱
		}
		return "redirect:/announcement/mine?parentId=62&status="+status;//添加草稿箱或者 直接发送
	}

	/**
	 * 公告详情（包含公告状态ID）
	 * @param id
	 * @param status
	 * @param model
	 * @return
	 */
	@Authorize(rules="announcement-mineDetail")
	@RequestMapping(value="/announcement/detail/{id}/{status}")
	public String myAnnouncementToDetail(@PathVariable int id,@PathVariable int status,Model model){
		Announcement announcement = announcementBiz.fetchAnnouncementById(id);
		int num = announcement.getApprover().getId();
		Department department =  employeeBiz.fetchEmployeeById(num).getDepartment();
		model.addAttribute("departmentName", department.getName());
		model.addAttribute("announcement", announcement);
		model.addAttribute("announcementStatus", status);//在jsp页面中  用jstl判断status是否存在  若存在则显示--返回我的公告--按钮
		return "announcement-detail";
	}
	/**
	 * 审批公告（Get）
	 * @param pageNum
	 * @param session
	 * @param model
	 * @return
	 */
	@Authorize(rules="announcement-approve")
	@RequestMapping(value="/announcement/approve")
	public String approveAnnouncement(
			@RequestParam(value="pageNum",defaultValue="1") int pageNum,
			HttpSession session,
			Model model){
		Employee employee = (Employee)session.getAttribute("user");
		int userId = employee.getId();
		Employee approve = employeeBiz.fetchEmployeeById(userId);
		Integer departmentId = approve.getDepartment().getId();
		//int totalPage = 
		int pageSize = 10;
		int totalCount = announcementBiz.findCountByPending(departmentId,1);
		int totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		List<Announcement> announcements = announcementBiz.findAnnouncementByPending(departmentId,1,pageNum,pageSize);
		model.addAttribute("announcements", announcements);
		model.addAttribute("pageNum",pageNum);
		model.addAttribute("totalPage", totalPage);
		return "announcement-approve";
	}
	
	/**
	 * 查看需审批公告详情
	 * @param announcementId
	 * @param model
	 * @return
	 */
	@Authorize(rules="announcement-approveDetail")
	@RequestMapping(value="/announcement/makeApprove")
	public String makeApprove(int announcementId,Model model){
		Announcement announcement = announcementBiz.fetchAnnouncementById(announcementId);
		int creatorId = announcement.getCreator().getId();
		Employee approve = employeeBiz.fetchEmployeeById(creatorId);
		String departmentName = approve.getDepartment().getName();
		model.addAttribute("announcement", announcement);
		model.addAttribute("departmentName",departmentName);
		return "announcement-makeApprove";
	}
	
	/**
	 * 审批公告（通过审批/驳回审批）
	 * @param id
	 * @param status
	 * @return
	 */
	@Authorize(rules="announcement-makeapprove")
	@RequestMapping(value="/announcement/makeApprove",method=RequestMethod.POST)
	public String submitApprove(int id, int status){
		announcementBiz.approveAnnouncement(id, status);
		return "redirect:/announcement/approve";
	}
}
